clear
%% Problem
%   'EqualMinima','Himmelblau','Sixhump','ModifiedRastrigin','Vincent',
%   'Shubert','Composition','IncreasingMinima','Rastrigin','Schaffer'.
ProblemSet.FuncType = 'Shubert';
ProblemSet.dimension = 2;
% ProblemSet.k = [3,4];
% ProblemSet.BaseFunc = 3;

[d, x_bound, ObjFunc, OptSet, precision_init, precision, ~, MaximalBudget, FileName]...
    = problem_setting(ProblemSet);

n_max_candidate = [6,10,15,20];
alpha_candidate = [0.1,0.2,0.3,0.4];
delta_candidate = [3,5,10,20];
iepsilon = 4;
epsilon = [0.1,0.01,0.001,0.0001];
OptSet.epsilon = epsilon(iepsilon);

%% problem
Problem.Dimension = d;
Problem.Domain = reshape(x_bound,[1,d*2]);
Problem.Sampling = @(n, region)Sampling_BoxConstraint(n, region, ObjFunc);
NewRegionNum = 2;
MaxPartitionDepth_d = ceil(log((x_bound(2,:)-x_bound(1,:))./precision_init)./log(NewRegionNum));
MaxPartitionDepth = sum(MaxPartitionDepth_d);
Problem.Partition = @(Region,SampleSet)Partition_BoxConstraint(Region, NewRegionNum, MaxPartitionDepth, SampleSet);
%% Algorithm
diff = fullfact([length(alpha_candidate),length(n_max_candidate),length(delta_candidate)]);
diff(:,1) = alpha_candidate(diff(:,1));
diff(:,2) = n_max_candidate(diff(:,2));
diff(:,3) = delta_candidate(diff(:,3));
AlgorithmM.StopCriteria = [1,MaximalBudget];
ClearRadius0 = (x_bound(2,:)-x_bound(1,:))./(NewRegionNum.^(MaxPartitionDepth_d));
ClearRadius = 2*min(ClearRadius0); % the maximum distance within a non-partitionable region
AlgorithmM.radius = ClearRadius;
AlgorithmM.local_search = @(starts,step,MaximalBudget)LocalSearch_test(ObjFunc,x_bound,starts,step,precision(iepsilon)/2,OptSet,MaximalBudget);
%% OPTIMIZATION
rep = 100;
OptObtained = zeros(size(diff,1),rep);
TotalBudget1 = zeros(size(diff,1),rep);
TotalBudget2 = zeros(size(diff,1),rep);
OptNoFound = zeros(size(diff,1),rep);
Times = zeros(size(diff,1),rep);
OptRemain = cell(size(diff,1),rep);
%%
for i = 1:size(diff,1)
    AlgorithmM.QuantileLevel = diff(i,1);
    AlgorithmM.MaxSampleSize = diff(i,2);
    AlgorithmM.NewBudget = diff(i,3);
    AlgorithmM.n0 = ceil(AlgorithmM.MaxSampleSize/3);
    for j = 1:rep
        disp([num2str(diff(i,:)),', replication: ',num2str(j),'.'])
        tic
        [optima, ~, SampleSet, ~, ls_budget, OptSetRemain]...
            = prsmmo_ls_test( Problem, AlgorithmM, OptSet );
        Times(i,j) = toc;
        disp(['time used: ',num2str(Times(i,j)),'.'])
        OptObtained(i,j) = size(optima,1);
        TotalBudget2(i,j) = ls_budget;
        TotalBudget1(i,j) = size(SampleSet,1) - ls_budget;
        OptRemain{i,j} = OptSetRemain;
        OptNoFound(i,j) = size(OptSetRemain,1);
    end
end
%%
clear i j OptSetRemain SampleSet ls_budget optima
save(FileName)
a=[mean(TotalBudget1)',mean(TotalBudget2)',mean(TotalBudget1+TotalBudget2)',var(TotalBudget1+TotalBudget2)'];
%% figures
TotalBudget = TotalBudget1+TotalBudget2;
figure()
maineffectsplot(TotalBudget,diff)
figure()
interactionplot(TotalBudget,diff)
%%
batch = 5;
value2test = TotalBudget;
value2test = reshape(value2test,[size(diff,1)*batch,rep/batch]);
value2test = mean(value2test,2);
diff2test = repmat(diff,[batch,1]);
% [value2test,lambda] = boxcox(value2test);
[p,tbl,stats] = anovan(value2test,diff2test,'model','interaction');
% stand_resid = stats.resid/stats.mse;


